package com.michaelbulava.TFS.tasks.vc;

import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.microsoft.tfs.core.clients.versioncontrol.GetItemsOptions;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.*;
import com.microsoft.tfs.core.clients.versioncontrol.specs.ItemSpec;
import com.microsoft.tfs.util.Check;

import java.text.MessageFormat;

/**
 * Created by MichaelA on 7/22/2015.
 */
public class QueryItemsExtendedTask extends BackgroundableTask{

    private final Workspace workspace;
    private final ItemSpec[] itemSpecs;
    private final DeletedState deletedState;
    private final ItemType itemType;
    private final GetItemsOptions options;
    private final String[] itemPropertyFilters;
    private ExtendedItem[][] extendedItems;
    private String name = null;

    private Throwable exception;
    private boolean completed = false;


    public QueryItemsExtendedTask(Project project, Workspace workspace, String path, ItemType itemType,
                                  DeletedState deletedState, RecursionType recursionType, GetItemsOptions options){
        this(project, workspace, new ItemSpec(path, recursionType), deletedState, itemType, options);
    }

    public QueryItemsExtendedTask(Project project, Workspace workspace, ItemSpec itemSpec, DeletedState deletedState,
                                  ItemType itemType, GetItemsOptions options) {
        this(project, workspace, new ItemSpec[] { itemSpec }, deletedState, itemType, options);
    }

    public QueryItemsExtendedTask(Project project, Workspace workspace, ItemSpec[] itemSpecs, DeletedState deletedState,
                                  ItemType itemType, GetItemsOptions options){
        this(project,workspace, itemSpecs, deletedState, itemType, options, null);
    }

    public QueryItemsExtendedTask(Project project, Workspace workspace, ItemSpec[] itemSpecs,
                                     DeletedState deletedState, ItemType itemType, GetItemsOptions options,
                                     String[] itemPropertyFilters) {
        super(project, createTitle(itemSpecs), false);
        Check.notNullOrEmpty(itemSpecs, "itemSpecs");
        Check.notNull(options, "options");

        this.workspace = workspace;
        this.itemSpecs = itemSpecs;
        this.deletedState = deletedState;
        this.itemType = itemType;
        this.options = options;
        this.itemPropertyFilters = itemPropertyFilters;
    }



    private static String createTitle(ItemSpec[] itemSpecs){
        if(itemSpecs.length == 1){
            String messageFormat = Messages.getString("QueryItemsExtendedCommand.SingleCommandTextFormat");
            return MessageFormat.format(messageFormat, new Object[]{itemSpecs[0].getItem()});
        }
        String messageFormat = Messages.getString("QueryItemsExtendedCommand.MultiCommandTextFormat");
        return MessageFormat.format(messageFormat, new Object[] { Integer.valueOf(itemSpecs.length) });
    }

    @Override
    protected boolean runInBackground() {
        return false;
    }

    public boolean isOk(){
        return exception == null;
    }

    @Override
    public void run(ProgressIndicator progressIndicator) {
        progressIndicator.setIndeterminate(true);
        completed = false;
        try{

            workspace.getClient().getExtendedItems(itemSpecs, deletedState, itemType, options);
            extendedItems = workspace.getExtendedItems(itemSpecs, deletedState, itemType, options, itemPropertyFilters);
        } catch(Throwable tr){
            this.exception = tr;
        } finally {
            completed = true;
        }
    }

    public ExtendedItem[][] getItems() {
        return extendedItems;
    }

    public Throwable getException(){
        return this.exception;
    }
}
